package com.sqisland.dash;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.sqisland.dash.SensorsService.LocalBinder;

public abstract class BaseActivity extends Activity {
  protected SensorsService sensorsService;
  protected boolean bound = false;
  protected ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
      LocalBinder binder = (LocalBinder) service;
      sensorsService = binder.getService();
      bound = true;
      onSensorServiceConnected();
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
      bound = false;
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public void onStart() {
    super.onStart();
    Intent intent = new Intent(this, SensorsService.class);
    bindService(intent, connection, Context.BIND_AUTO_CREATE);
  }

  @Override
  public void onStop() {
    super.onStop();
    if (bound) {
      unbindService(connection);
      bound = false;
    }
  }

  protected void onSensorServiceConnected() {
  }
}